package com.orcsystem.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.orcsystem.common.utils.PageUtils;
import com.orcsystem.common.utils.Query;
import com.orcsystem.member.dao.MemberDao;
import com.orcsystem.member.dao.MemberLevelDao;
import com.orcsystem.member.entity.MemberEntity;
import com.orcsystem.member.entity.MemberLevelEntity;
import com.orcsystem.member.exception.EmailExistException;
import com.orcsystem.member.exception.MobileExistException;
import com.orcsystem.member.exception.UserNameExistException;
import com.orcsystem.member.service.MemberService;
import com.orcsystem.member.vo.MemberLoginVo;
import com.orcsystem.member.vo.MemberRegistVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {
    @Autowired
    MemberLevelDao memberLevelDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void regist(MemberRegistVo vo) {
        // 检查 用户名 和 手机号是否唯一
        checkMobileUnique(vo.getMobile());
        checkUserNameUnique(vo.getUsername());

        MemberLevelEntity levelEntity = memberLevelDao.getDefaultLevel();   // 查询会员注册默认等级
        MemberEntity entity = new MemberEntity();
        // 设置默认会员等级
        entity.setLevelId(levelEntity.getId());
        // 设置 手机号、账号、密码
        entity.setMobile(vo.getMobile());
        entity.setUsername(vo.getUsername());
        // 密码加密
        BCryptPasswordEncoder passwordEncoder= new BCryptPasswordEncoder();
        String password = passwordEncoder.encode(vo.getPassword());
        entity.setPassword(password);
        baseMapper.insert(entity);
    }

    @Override
    public void checkEmailUnique(String email) throws EmailExistException {
        throw new EmailExistException();
    }

    @Override
    public void checkMobileUnique(String mobile) throws MobileExistException {
        Integer mobileNum = this.baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("mobile", mobile));
        if (mobileNum > 0) {
            throw new MobileExistException();
        }

    }

    @Override
    public void checkUserNameUnique(String userName) throws UserNameExistException {
        Integer usernameNum = this.baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("username", userName));
        if (usernameNum > 0){
            throw new UserNameExistException();
        }
    }

    @Override
    public MemberEntity login(MemberLoginVo vo) {

        MemberEntity entity = baseMapper.selectOne(new QueryWrapper<MemberEntity>().eq("username", vo.getLoginacct()).or().eq("mobile", vo.getLoginacct()));
        if (entity == null){
           return null;
        }else {
            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            boolean matches = encoder.matches(vo.getPassword(), entity.getPassword());
            if (!matches){
                return null;
            }else {
                return entity;
            }
        }
    }

}
